<?php
require_once($application_folder."/controllers/navigator.php");
class Crop extends navigator 
{

    function __construct()
    {
        parent::__construct();
        
        /************************************************************
        * CHECK LOGIN
        *************************************************************/
        if(!$this->session->userdata('logged_in')){
            redirect(base_url());
        }
         
    }

    function saveThumbnail(){

        $params = array( 
        'thumb_image_prefix' => 'thumb_', 
        'max_file' => '3', 
        'max_width' => '500', 
        'thumb_width' => $_POST['w'], 
        'thumb_height' => $_POST['h'], 
        'original_image_name' => $_POST['original_image_name'], 
        'original_image_location' => './tempatt/',
        'thumb_image_name' => $_POST['original_image_name'], 
        'thumb_image_location' => './tempatt/'
        );
        $this->load->library('cropimage', $params);

        //CHECK IF IMAGE EXISTS
        if(!$this->cropimage->checkFileExisit()){
            echo json_encode(array('action' => FALSE, 'msg' => 'File not found'));
        }

        //SET COORDINATE AND CROP IMAGE
        $coordinate = array(
        'x1' => $_POST['x1'],
        'y1' => $_POST['y1'],
        'x2' => $_POST['x2'],
        'y2' => $_POST['y2'],
        'w' => $_POST['w'],
        'h' => $_POST['h']
        );
        if($this->cropimage->createThumbnail($coordinate)){

            $mediaId = $this->writeMediaToDb();
            if($mediaId != FALSE){
                echo json_encode(array('action' => TRUE, 'msg' => 'File created', 'mediaId' => $mediaId, 'fileName' => $this->cropimage->getThumbImageName())); 
            }else{
                echo json_encode(array('action' => TRUE, 'msg' => 'File created but not writed to DB'));
            }



        }else{
            echo json_encode(array('action' => FALSE, 'msg' => 'Error ocured'));  
        }

    }

    function writeMediaToDb(){

        $this->load->model('cropm');

        $params = array(
        'created'=> time(),
        'file'=> $this->cropimage->getThumbImageName(),
        'directory_id'=> 1,
        'user_id'=> $this->session->userdata('id'),
        'croped'=> TRUE
        );
        
        RETURN $this->cropm->writeThumbToDb($params);


    }


    /***    
    UNIT TESTING
    ***/

    function testCropClass(){

        //        Set starting params
        $original_image_name = 'p16i3cmjum1c8npar1kip2fh8rv1.jpg';
        $w = 299;
        $h = 149;        
        $x1 = 86;
        $x2 = 385;
        $y1 = 40;
        $y2 = 189;

        //        Create params and initialze Crop Class
        $params = array( 
        'thumb_image_prefix' => 'thumb_', 
        'max_file' => '3', 
        'max_width' => '500', 
        'thumb_width' => $w, 
        'thumb_height' => $h, 
        'original_image_name' => $original_image_name, 
        'original_image_location' => './tempatt/',
        'thumb_image_name' => $original_image_name, 
        'thumb_image_location' => './tempatt/thumbnail/'
        );
        $this->load->library('cropimage', $params);        

        //        START TEST

        //        CHECK IF IMAGE EXISTS
        if($this->cropimage->checkFileExisit()){
            echo '<br />Original image is found.';
            //echo '<br />'.$this->cropimage->getFullThumbPath();
        }else{
            echo '<br />File not found';
            echo '<br />Thumb will bw saved in: '.$this->cropimage->getFullImagePath();
            return FALSE;
        }

        //        SET COORDINATE AND CROP IMAGE
        $coordinate = array(
        'x1' => $x1,
        'y1' => $y1,
        'x2' => $x2,
        'y2' => $y2,
        'w' => $w,
        'h' => $h
        );

        //        CROP IMAGE AND READ RESULTS
        if($this->cropimage->createThumbnail($coordinate)){
            echo '<br />Crop successfull';
            echo '<br /><img src="'.base_url().$this->cropimage->getFullThumbPath().'" />';
            echo '<br />File: '.$this->cropimage->getThumbImageName();
        }else{
            echo '<br />Crop failed';
            return FALSE;
        }         

    }

    function testWriteMediaToDb(){

        /*        NOTE
        *
        *         Method writeMediaToDb need comments to test this method
        * */
        
        //        START TEST
        $this->load->model('cropm');
        
        $params = array(
        'created'=> time(),
        'file'=> 'thumb_1326896556_p16i3cmjum1c8npar1kip2fh8rv1.jpg',
        'directory_id'=> 1,
        'user_id'=> 2,
        'croped'=> TRUE
        );
        
        if($this->cropm->writeThumbToDb($params)){
            echo 'Writed to media table.'; 
        }else{
            echo 'File writed to DB';
        }

    }

}